package com.hp.service;


import com.hp.Utils.UserPhoto;
import com.hp.dao.UserMapper;
import com.hp.pojo.Chat;
import com.hp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Yang
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;


    /*
    验证用户登录是否成功 用户
    返回用户的数据
     */
    public Map<String,Object> userLogin(String userPhone, String userPassword, HttpSession session){
        HashMap<String, Object> result = new HashMap<>();
        User user = userMapper.getUsersByPhone(userPhone);
        System.out.println(user);
        if(user == null) {
            result.put("msg", "用户名或密码错误");
            result.put("status", 0);
//            return result;
        } else if(user.getUserPassword().equals(userPassword)){
            session.setAttribute("userId",user.getUserId());
            result.put("msg","登陆成功");
            result.put("status",1);
            result.put("data",user);
//            return result;
        }else if(user.getUserStatus() == 0) {
            result.put("msg","用户已注销");
            result.put("status",0);
//            return result;
        }
        return result;
    }

    /*
    注册or新增一条用户记录 用户
     */
    public HashMap<String, Object> addUser(User user){
        HashMap<String, Object> result = new HashMap<>();
        int res = userMapper.addUser(user);
        if(res == 0){
            result.put("msg","新用户注册失败");
            result.put("status",0);
            return result;
        }
        result.put("userId", userMapper.getUsersByPhone(user.getUserPhone()).getUserId());
        result.put("msg","新用户注册成功");
        result.put("status",1);
        return result;
    }

    /*
    根据用户id删除一条用户记录 用户
     */
    public HashMap<String, Object> deleteUserById(int userId){
        HashMap<String, Object> result = new HashMap<>();
        int res = userMapper.deleteUserById(userId);
        if(res == 0){
            result.put("msg","用户注销失败");
            result.put("status",0);
            return result;
        }
        result.put("msg","用户注销成功");
        result.put("status",1);
        return result;
    }

    /*
    更新一条用户记录 用户
    */
    public HashMap<String, Object> updateUserById(User user){
        HashMap<String, Object> result = new HashMap<>();
        int res = userMapper.updateUserById(user);
        if(res == 0){
            result.put("msg","更新用户信息失败");
            result.put("status",0);
            return result;
        }
        result.put("msg","成功更新用户信息");
        result.put("status",1);
        return result;
    }

    /*
    更新一条用户的部分记录
     */
    public HashMap<String, Object> updateUserPartById(int userId,String userNickname,String userName,String userPhone,int userSex){
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("userNickname", userNickname);
        map.put("userName", userName);
        map.put("userPhone", userPhone);
        map.put("userSex", userSex);
        int res = userMapper.updateUserPartById(map);
        if(res == 0){
            result.put("msg","更新用户信息失败");
            result.put("status",0);
            return result;
        }
        result.put("msg","成功更新用户信息");
        result.put("status",1);
        return result;
    }
    /*
    通过用户id获取用户记录信息 用户
     */
    public HashMap<String, Object> getUserById(int userId){
        HashMap<String, Object> result = new HashMap<>();
        User user = userMapper.getUserById(userId);
        if(user == null){
            result.put("msg","获取用户信息失败");
            result.put("status",0);
            result.put("data",user);
            return result;
        }
        result.put("msg","成功更新用户信息");
        result.put("status",1);
        result.put("data",user);
        return result;
    }

    /*
    用户上传头像 用户
     */
    public Map<String,Object> uploadUserPhoto(int userId, MultipartFile photo) throws IOException {
        HashMap<String, Object> result = new HashMap<>();
        Map<String,Object> res = UserPhoto.uploadPhoto(photo,userId);
        if(res.get("status").equals(1)){
            User user = userMapper.getUserById(userId);
            user.setUserPhoto((String) res.get("fileName"));
            int sqlResult = userMapper.updateUserById(user);
            if(sqlResult == 1){
                result.put("status",1);
                result.put("msg","头像更新成功");
                return result;
            }else{
                result.put("status",0);
                result.put("msg","更新用户数据失败");
                return result;
            }
        }else{
            result.put("status",0);
            result.put("msg",res.get("msg"));
            return result;
        }
    }

    /*
    获取所有用户记录 管理
     */
    public HashMap<String, Object> getAllUsers(){
        HashMap<String, Object> result = new HashMap<>();
        List<User> userList = null;
        try{
            userList = userMapper.getAllUsers();
            result.put("msg","获取用户列表信息成功");
            result.put("status",1);
            result.put("data",userList);
        }catch (Exception e){
            result.put("msg","获取用户列表信息失败");
            result.put("status",0);
            result.put("data",userList);
        }
        return result;
    }

    /*
    通过用户昵称获取用户列表信息 管理
     */
    public HashMap<String, Object> getUsersByNickname(String userNickname){
        HashMap<String, Object> result = new HashMap<>();
        List<User> userList = null;
        try{
            userList = userMapper.getUsersByNickname(userNickname);
            result.put("msg","获取用户列表信息成功");
            result.put("status",1);
            result.put("data",userList);
        }catch (Exception e){
            result.put("msg","获取用户列表信息失败");
            result.put("status",0);
            result.put("data",userList);
        }
        return result;
    }

    /*
    通过用户电话查找用户列表信息 管理
     */
    public HashMap<String, Object> getUsersByPhone(String userPhone){
        HashMap<String, Object> result = new HashMap<>();
        User user = null;
        try{
            user = userMapper.getUsersByPhone(userPhone);
            result.put("msg","获取用户列表信息成功");
            result.put("status",1);
            result.put("data",user);
        }catch (Exception e){
            result.put("msg","获取用户列表信息失败");
            result.put("status",0);
            result.put("data",user);
        }
        return result;
    }

    public Map<String, Object> AliveUserById(int userId) {
        HashMap<String,Object> map=new HashMap<>();
        if (userMapper.AliveUserById(userId)==1){
            map.put("msg","激活成功！");
        }
        else {
            map.put("msg","激活失败！");
        }
        return map;
    }

    public Map<String, Object> checkPhone(String userPhone) {
        HashMap<String, Object> map = new HashMap<>();
        if (userMapper.getUsersByPhone(userPhone) != null) {
            map.put("msg", "该手机号已被注册");
            map.put("status", 0);
        } else {
            map.put("status", 1);
        }
        return map;
    }

    public Map<String, Object> getAgent(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("agentUser",userMapper.getAgent());
        return map;
    }
}
